package com.example.gpstest.service;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.Build;
import android.os.IBinder;

import androidx.core.app.ActivityCompat;

import java.util.ArrayList;

public class GpsService extends Service {
    public GpsService() {
    }
    private static int STAR_MAX_SNR = 50;//搜星最大有效snr 上限
    private static int STAR_MIN_SNR = 30;//最低有效snr 下限
    private static int MIN_STAR_NUM = 4;//最低有效卫星数量
    private LocationManager locationManager;
    private ArrayList<GpsSatellite> numSatelliteList = new ArrayList();//有效搜星数量

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    Activity#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
                //return;
            }
        }else{
            locationManager.addGpsStatusListener(statusListener);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {// GPS状态变化时的回调，获取当前状态

            if (ActivityCompat.checkSelfPermission(GpsService.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GpsService.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            if (locationManager != null) {
                GpsStatus status = locationManager.getGpsStatus(null);
                // 获取卫星状态相关数据  判断 室内还是室外
                GetGPSStatus(event, status);
            }

        }
    };

    private void GetGPSStatus(final int event, final GpsStatus status) {
//        ThreadPoolExecutor.execute(new Runnable() {
//            @Override
//            public void run() {
//
//                if (status == null) {
//
//                } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
//                    //   获取最大的卫星数（这个只是一个预设值）
//                    int maxSatellites = status.getMaxSatellites();
//                    Iterator<GpsSatellite> it = status.getSatellites().iterator();
//                    numSatelliteList.clear();
//
//                    int count = 0;
//                    while (it.hasNext() && count <= maxSatellites) {
//                        GpsSatellite s = it.next();
//                        if (s.getSnr() >= STAR_MIN_SNR && s.getSnr() <= STAR_MAX_SNR) {//
//                            //只有信躁比不为0的时候才算搜到了星 此处统计SNR在30-50间的卫星数， 重要： 需要清除星历 重测 冷启动
//                            numSatelliteList.add(s);
//                            //  SCLog.i(TAG, "s.getSnr()" + " snr--- " + s.getSnr());
//                            count++;
//                        }
//
//                    }
//                    if (numSatelliteList.size() >= MIN_STAR_NUM) {
//
//                        //todo 此处判断搜星结果 为有效信号
//
//                    }
//                } else if (event == GpsStatus.GPS_EVENT_STARTED) {
//
//                    //定位启动
//                } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
//                    //定位结束
//
//                }
//            }
//        });

    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
